package core.controls;
import ibm.widgets.WImage;
import ibm.widgets.WLink;

import com.rational.test.ft.object.interfaces.TestObject;
import com.rational.test.util.regex.Regex;

import core.exceptions.ResultFailureException;
import core.testcases.TestObj;
/**
 * Description   : Functional Test Script
 * @author angel.tsvetkov
 */
public class Link extends Control<WLink>
{
	public Link(String propertyValue, String propertyName, String objClass,
			TestObject parent, String descriptiveName) {
		super(propertyValue, propertyName, objClass, parent, descriptiveName);
		// TODO Auto-generated constructor stub
	}

	/**
	 * Description : Click link.
	 * 
	 * @param linkText - text of the link
	 * 
	 * @param parent - area in which the link belongs.
	 */
	public boolean click()
			throws ResultFailureException {
		// Initialize variables
		boolean result = false;
		WLink linkToClick = null;

		// Perform click link
		try {
			// Allocate link
			linkToClick = this.get();

			// Click it
			linkToClick.click();

			result = true;
		} catch (Exception e) {
			throw new ResultFailureException(
					"Unexpected assertion on clicking of " + linkToClick.getDescriptiveName()
							+ " link!");
		}
		finally
		{
			// Unregister object
			linkToClick.unregister();
		}
		// TODO: Check whether this sleep is needed
		sleep(3);

		return result;
	}
	
	public boolean clickLink()
	{
        boolean result = false;
        String taskReport = null;

        try
        {
            taskReport = " \n Click " + this.getDescriptiveName();
            this.click();
            result = true;
        }
        catch(ResultFailureException e)
        {
        	setErrorComment("Problem appear in pressing of button with prop name: " + this.PropName() + " and prop value: " + this.PropValue(),e);
        }
        
        setTaskResult("Task: Click " + this.getDescriptiveName() , result, taskReport);

        return result;
	}

	@Override
	public WLink get() {
		if(this.isCognosControl())
		{
			TestObject parent = this.getParent();
			this.obj = new WLink(parent.getChildren()[0]);
		}
		else
		{
			Regex toSearch = new Regex(this.PropValue(),Regex.MATCH_MULTILINE);
			this.obj = new WLink(toSearch,this.PropName(),this.getObjClass(),this.getParent());
		}
		return this.obj;
	}

	@Override
	public boolean isEnabled() {
		return this.get().isEnabled();
	}

	@Override
	public boolean isExist() {
		return this.get().ensureObjectIsVisible();
	}
	
	

}

